package com.example.demo.mcp.webserver;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.ai.chat.model.ToolContext;

import java.util.Map;

/**
 * @author zwg
 * @date 2025/9/24 22:14
 */
public class SampleClient {
    private final McpClientTransport transport;

    public SampleClient(McpClientTransport transport) {
        this.transport = transport;
    }

    public void run() {

        var client = McpClient.sync(this.transport)
                .sampling(request -> {
                    System.out.println("Received a new message: " + request);
                    return McpSchema.CreateMessageResult.builder()
                            .content(new McpSchema.TextContent("Bla bla bla"))
                            .build();
                })
                .build();

        client.initialize();

        client.ping();

        // List and demonstrate tools
        McpSchema.ListToolsResult toolsList = client.listTools();
        System.out.println("Available Tools = " + toolsList);

        McpSchema.CallToolResult weatherForcastResult = client.callTool(new McpSchema.CallToolRequest("getTemperature",
                Map.of("latitude", "47.6062", "longitude", "-122.3321", "toolContext", new ToolContext(Map.of()))));
        System.out.println("Weather Forcast: " + weatherForcastResult);

        client.closeGracefully();

    }
}
